﻿-- petardy funkcji
function launchPetarda(x,y,z)
	local petarda=createObject(3790,x,y,z-3,0,90,0)
	local _time=math.random(5000,7000)/2
	local x2=x+math.random(-2,2)
	local y2=y+math.random(-2,2)
	moveObject(petarda,_time,x2,y2,z+math.random(65,95),0,0,180,"InQuad")
	setTimer(boomPetarde,_time,1,petarda)
end

-- petardy graczy
for k,v in ipairs(getElementsByType("player"))do
	removeElementData(v,"petarda")
	removeElementData(v,"cannotPetarda")
end
addCommandHandler("petarda",
	function(player)
		if(isPetardyUnlocked)then
			if not(getPedOccupiedVehicle(player))and(isPedOnGround(player))then
				if(getElementData(player,"petarda"))then
					outputChatBox("* Postawiłeś już petardę! Maksymalnie możesz mieć jedną :)",player,50,150,50)
				elseif(getElementData(player,"cannotPetarda"))then
					outputChatBox("* Nie możesz postawić petardy, musisz odczekać minimum 2 minuty od wystrzelenia poprzedniej! :D",player,50,150,50)
				else
				local x,y,z=getElementPosition(player)
				local petarda=createObject(3790,x,y,z-3)
				setElementData(player,"petarda",petarda)
				setElementRotation(petarda,0,90,0)
				moveObject(petarda,1000,x,y,z+0.2,0,0,360,"OutQuad")
				giveWeapon(player,40,1,true)
				bindKey(player,"fire","down",remotePetarda)
				end
			else
				outputChatBox("* Musisz być bez pojazdu i na ziemii!",player,50,150,50)
			end
		end
	end
)

function odpalPetarde(petarda)
local x,y,z=getElementPosition(petarda)
local _time=math.random(5000,7000)/2
local x2=x+math.random(-2,2)
local y2=y+math.random(-2,2)
moveObject(petarda,_time,x2,y2,z+math.random(65,95),0,0,180,"InQuad")
setTimer(boomPetarde,_time,1,petarda,flame)
end

function remotePetarda(player)
	if(getElementData(player,"petarda"))then
		local petarda=getElementData(player,"petarda")
		outputChatBox("* Wystrzeliłeś petardę! :D",player,50,150,50)
		odpalPetarde(petarda)
		removeElementData(player,"petarda")
		setElementData(player,"cannotPetarda",true)
		setTimer(removeElementData,2*60*1000,1,player,"cannotPetarda")
		setTimer(outputChatBox,2*60*1000,1,"* Możesz już postawić nową petardę! :)",player,50,150,50)
	end
end

-- petardy shared
function setPetardyUnlocked(bool)
	isPetardyUnlocked=bool
end
expTable={0,2,3,4,6,7,8,9,10}
function boomPetarde(petarda)  
	local x,y,z=getElementPosition(petarda)
	local boomType=expTable[math.random(1,9)]
	createExplosion(x,y,z,boomType)
	destroyElement(petarda)
  
	local colorTable={}
	colorTable.r=0
	colorTable.g=0
	colorTable.b=0
	for k,v in pairs(colorTable)do
		colorTable[k]=math.random(50,150)
	end
	for i=0,math.random(4,8) do
		boomParticle(x,y,z,colorTable.r,colorTable.g,colorTable.b)
	end
end
function boomParticle(x,y,z,r,g,b)
	local object=createObject(327,x,y,z)
	setElementAlpha(object,0)
	local marker=createMarker(0,0,0,"corona",3,r,g,b,200)
	attachElements(marker,object)
	moveObject(object,math.random(5000,10000)/2,x+math.random(-100,100),y+math.random(-100,100),z+math.random(-100,100),0,0,0,"OutQuad")
	setElementData(marker,"bParticle",object)
end
setTimer(
	function()
		for k,v in ipairs(getElementsByType("marker"))do
			if(getElementData(v,"bParticle"))then
				local r,g,b,alpha=getMarkerColor(v)
				setElementAlpha(v,alpha-20)
				local bParticle=getElementData(v,"bParticle")
				local x,y,z=getElementPosition(bParticle)
				setElementPosition(bParticle,x,y,z-1.5)
				if(alpha==0)then
					destroyElement(bParticle)
					destroyElement(v)
				end
			end
		end
	end
,200,0)
